#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/5/11 11:11
# @Author : 闫峰
# @File    : voucher.py
import time

import requests

from third_services.shopee.base_tool import shop_auth, db, shop_auth_shop
from utils.otherutils.json_format import json_format
from third_services.shopee.common.common_func import get_timestamp, get_access_token, get_shop_id


class Voucher:
    base_url = "https://partner.shopeemobile.com"
    partner_id = 2001656

    def get_voucher(self, ordersourceid, voucher_id, apiService="/api/v2/voucher/get_voucher"):
        url = self.base_url + apiService
        payload = {}
        headers = {}
        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid),
            "voucher_id": voucher_id
        }
        response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                    allow_redirects=False).json()
        while True:
            if response['error'] == 'error_sign':
                param = {
                    "partner_id": self.partner_id,
                    "timestamp": get_timestamp(),
                    "access_token": get_access_token(ordersourceid),
                    "sign": shop_auth_shop(ordersourceid, apiService),
                    "shop_id": get_shop_id(ordersourceid),
                    "voucher_id": voucher_id
                }
                response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                            allow_redirects=False).json()
            else:

                break
        return response

    def get_voucher_list(self, ordersourceid, page_no, apiService="/api/v2/voucher/get_voucher_list"):
        url = self.base_url + apiService
        payload = {}
        headers = {}
        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid),
            "status": "expired",
            "page_no": page_no,
            "page_size": 100
        }
        response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                    allow_redirects=False).json()
        count = 0
        while True:
            if count <= 15:
                if response['error'] == 'error_sign' or response['error'] == "error_param":
                    param = {
                        "partner_id": self.partner_id,
                        "timestamp": get_timestamp(),
                        "access_token": get_access_token(ordersourceid),
                        "sign": shop_auth_shop(ordersourceid, apiService),
                        "shop_id": get_shop_id(ordersourceid),
                        "status": "all",
                        "page_no": page_no,
                        "page_size": 100
                    }

                    response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                                allow_redirects=False).json()
                    count += 1
                else:
                    break
            else:
                return response
        return response

    def add_voucher(self, ordersourceid, apiService="/api/v2/voucher/add_voucher"):
        url = self.base_url + apiService
        payload = {
            "discount_amount": 13.99,
            "display_channel_list": [],
            "end_time": 1688140799,
            "item_id_list": [
                22744128427
            ],
            "min_basket_price": 100.0,
            "reward_type": 1,
            "start_time": 1685635200,
            "usage_quantity": 123,
            "voucher_code": "ACV3G",
            "voucher_name": "ceshi001",
            "voucher_type": 2
        }
        headers = {}
        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid)
        }
        response = requests.request("POST", url, headers=headers, json=payload, params=param,
                                    allow_redirects=False).json()
        while True:
            if response['error'] == 'error_sign':
                payload = {
                    "discount_amount": 13.99,
                    "display_channel_list": [],
                    "end_time": 1688140799,
                    "item_id_list": [
                        22744128427
                    ],
                    "min_basket_price": 100.0,
                    "reward_type": 1,
                    "start_time": 1685635200,
                    "usage_quantity": 123,
                    "voucher_code": "ACV3G",
                    "voucher_name": "ceshi001",
                    "voucher_type": 2
                }
                param = {
                    "partner_id": self.partner_id,
                    "timestamp": get_timestamp(),
                    "access_token": get_access_token(ordersourceid),
                    "sign": shop_auth_shop(ordersourceid, apiService),
                    "shop_id": get_shop_id(ordersourceid)
                }
                response = requests.request("POST", url, headers=headers, json=payload, params=param,
                                            allow_redirects=False).json()
            else:

                break
        return response


if __name__ == '__main__':
    Voucher = Voucher()
    # 通过sourceid查询店铺有哪些优惠券
    res = Voucher.get_voucher_list(14025,1)
    # 通过sourceid voucher_id 获取参加优惠券的item_id
    # 582201934757952  490000504635392
    # res = Voucher.get_voucher(14025, 236380530098176)
    # res = Voucher.add_voucher(14267)
    res = json_format(res)
    print(res)
